package fkti1381bkh;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

public class Paper extends Canvas {
	private Root head;
	private Root cur;
	/**
	 * Set head
	 * @param head new head
	 */
	public void sethead(Root head){this.head=head;}
	/**
	 * Get head
	 * @return head
	 */
	public Root gethead(){return head;}
	/**
	 * Set current
	 * @param cur new current
	 */
	public void setcur(Root cur){this.cur=cur;}
	/**
	 * Get current.
	 * @return current
	 */
	public Root getcur(){return cur;}
	/**
	 * Paint graphics
	 */
	public void paint(Graphics g)
	{
		print_tree(g,400,50,1,head);
	}
	/**
	 * Print tree.
	 * @param g Graphics
	 * @param x coordinate x
	 * @param y coordinate y
	 * @param m determines the angle between the sons
	 * @param hd head
	 */
	public void print_tree(Graphics g, int x, int y, int m, Root hd) 
	{
		 if(hd==null) return;
		 int sdvx=4+5/m;
		 int sdvy=(int)Math.pow((float)100-Math.pow(sdvx,2),(float)1/2);
		 String result="";
		 result=String.valueOf(hd.geta());
		 g.setColor(Color.BLUE);
		 if(hd==cur)
		 {
			 g.setColor(Color.red);
			 g.drawOval(x, y, 20, 20);
		 }
		 else
		 {
			 g.drawOval(x, y, 20, 20);
		 }
		 g.setColor(Color.BLACK);
		 g.drawString(result, x+7, y+14); 
		 g.setColor(Color.BLUE);
		 m*=2;
		 if(hd.getLeft()!=null)
		 {
			 g.drawLine(x+10-sdvx, y+10+sdvy, x-300/m+10+sdvx, y+85-sdvy);
			 print_tree(g,x-300/m,y+75,m,hd.getLeft());
		 }
		 if(hd.getRight()!=null)
		 {
			 g.drawLine(x+10+sdvx, y+10+sdvy, x+300/m+10-sdvx, y+85-sdvy);
			 print_tree(g,x+300/m,y+75,m,hd.getRight());
		 }
	}
}
